<template>
  <div class="login-container">
    <img src="./logo.png" class="img"/>
    <div class="main">
      <div class="main-item">
        <el-form ref="form" :rules="rules" :model="loginform" label-width="80px" class="login-form">
          <h2 class="login-title">欢迎使用！</h2>
          <el-form-item label="账号" prop="phone">
            <el-input v-model="loginform.phone" placeholder="请输入电话号码"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="userpaw">
            <el-input v-model="loginform.userpaw" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm()">登录</el-button>
            <el-button type="primary" @click="toRegister()">注册</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
    <div class="footer">
      <a href="#">客服中心</a>
      |
      <a href="#">联系我们</a>
    </div>
  </div>
</template>
<script>
  const axios = require("axios");
  import Common from '../../components/common/Common';
  import ls from '../../components/common/CommonJS.js'

  export default {
    data() {
      return {
        loginform: {
          phone: "",
          userpaw: ""
        },
        rules: {
          phone: [
            {required: true, message: "账号不能为空", trigger: 'blur'},
            {min: 11, max: 11, message: "请正确输入电话号码", trigger: 'blur'}
          ],
          userpaw: [
            {required: true, message: "密码不能为空", trigger: 'blur'},
            {min: 3, max: 10, message: "密码3-10位", trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm() {
        const that = this;
        console.log("发送"+Common.API,Common.name);
        var loginform = this.loginform;
        axios.post(Common.API + 'login/login', {
          phone: this.loginform.phone,
          userpaw: this.loginform.userpaw
        })
          .then(function (res) {
            console.log("登录结果1：", res);
            console.log("登录结果2：", res.data);
            console.log("登录结果3：", res.data.data);
            console.log("企业信息", res.data.data.enterpriseInfo);
            console.log("用户信息", res.data.data.userInfo);


            Common.userInfo = res.data.data.userInfo;//无论是谁登录，这里都有记录
            if (res.data.data.userInfo.available == '待审核') {
              that.$message('请等待账号审核通过，注册的账号会在三天内完成审核！');
              return;
            }

            sessionStorage.setItem("userid", res.data.data.userInfo.id);
            sessionStorage.setItem("status", res.data.data.userInfo.status);
            ls.setItem("userInfo", res.data.data.userInfo);

            var loginStatus = res.data.data.userInfo.status;
            if (loginStatus == 1)//确定是卖方登录
            {
              // Common.userInfoSale = res.data.data.userInfo;
              ls.setItem("userInfoSale", res.data.data.userInfo);
              // Common.enterpriseInfoSale = res.data.data.enterpriseInfo;
              ls.setItem("enterpriseInfoSale", res.data.data.enterpriseInfo);

            } else if (loginStatus == 0) {//确定是买方登录
              // Common.userInfoBuy = res.data.data.userInfo;
              ls.setItem("userInfoBuy", res.data.data.userInfo);
              // Common.enterpriseInfoBuy = res.data.data.enterpriseInfo
              ls.setItem("enterpriseInfoBuy", res.data.data.enterpriseInfo);
            }
            if (loginStatus == 1 || loginStatus == 0) {//买方或者卖方登录，跳转到主界面
              that.$router.push("/Homepage")
            }
            console.log(loginStatus)
            switch (loginStatus) {//=0表示买方、1表示卖方、7表示超管、6表示资讯发布人、5表示资讯审核人、3表示交易审核人、2表示注册账号审核人
              case '2'://2表示注册账号审核人，账号是9开头
                that.$router.push("/AuditRegister");
                break;
              case '3'://3表示交易审核人
                that.$router.push("/AuditProp");
                break;
              case '5'://资讯审核
                that.$router.push("/AuditNews");
                break;
              case '6':////资讯发布人
                that.$router.push("/ReleaseNew");
                break;
              case '7'://7表示超管
                that.$router.push("/Admin");
                break;
            }


          })
          .catch(function (error) {
            console.log("fu:"+error);
          });
      },
      toRegister(){
        this.$router.push("/registerIndex");
      }
    }
  };
</script>

<style acoped>

  .main-item {

    /*position: absolute;*/
    /*margin-right: 20%;*/
    /*margin:0 auto;*/
    margin-left: 50%;
  }

  .login-form {
    width: 350px;
    background-color: rgba(255, 255, 255, 0.9); /* 透明背景色 */
    padding: 30px;

    /*margin-left: 65%;*/
    /*margin-right: 10%;*/
    /*float: right;*/

  }

  .main {
    width: 80%;
    height: 350px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin: 20px auto; /* 上下间距160px，左右自动居中*/
    background: url("./background.jpeg") no-repeat;
    border-radius: 5px; /* 圆角 */
    box-shadow: 10px 10px 10px rgba(0, 0, 0, .5);
    background-size:100% 100%;
    background-attachment:fixed;
    /*padding-left: 20%;*/
  }

  /* 背景 */
  .login-container {
    width: 100%;
    height: 100%;
    background-color: white;
  }

  /* 标题 */
  .login-title {
    color: #303133;
    text-align: center;
    margin-bottom: 1em;
  }

  .footer {
    margin-top: 100px;
    text-align: center;
  }

  .footer a {
    text-decoration: none;
  }
</style>

